package com.nhaycungbibi.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.tubeilike.entity.Tube;
import com.tubeilike.model.TubeModel;

@SuppressWarnings("serial")
public class AdVideoController extends HttpServlet {

	private BlobstoreService blobstoreService = BlobstoreServiceFactory
			.getBlobstoreService();

	@SuppressWarnings("unused")
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		String path = ((HttpServletRequest) request).getRequestURI();
		String action = "";
		String[] splittedURI = path.split("/");
		// if length = 3, this is for action request.
		if (splittedURI.length == 4) {
			action = splittedURI[splittedURI.length - 1];
			System.out.println("Do action : " + action);
			// edit video information.
			if (action.equalsIgnoreCase("edit")) {
				if (request.getParameter("id") != null) {
					response.getWriter().print(
							"Edit video + " + request.getParameter("id"));
					Tube tub = TubeModel
							.getByTubeId(request.getParameter("id"));
					if (tub != null) {
						request.setAttribute("detail", tub);
						request.getRequestDispatcher("/video_edit.jsp")
								.forward(request, response);
					} else {
						response.getWriter().print(
								"Video is not exits or has been deleted.");
					}
				}

			}
		}
		// if length = 4, this is view detail playlist request.
		else if (splittedURI.length == 5) {
			response.getWriter().println("Length = 5.");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		// UserService userService = UserServiceFactory.getUserService();
		// User user = userService.getCurrentUser();
		// if (user != null) {
		String action = req.getParameter("action");
		if (action.equalsIgnoreCase("edit")) {
			if (req.getParameter("tubeId") != null) {
				Tube tub = TubeModel.getByTubeId(req.getParameter("tubeId"));
				if (tub != null) {
					if (req.getParameter("title") != null) {
						tub.setTitle(new Text(req.getParameter("title")));
					}
					if (req.getParameter("description") != null) {
						tub.setDescription(new Text(req
								.getParameter("description")));
					}
					Map<String, List<BlobKey>> blobs = blobstoreService
							.getUploads(req);
					List<BlobKey> blobKeys = blobs.get("myFile");
					if (blobKeys != null && blobKeys.size() > 0) {
						tub.setThumbImageUrl(new Text("/image?key=" + blobKeys.get(0)
								.getKeyString()));
					}
					TubeModel.update(tub);
					TubeModel.closePM();
					req.setAttribute("detail", tub);
					req.getRequestDispatcher("/video_edit.jsp").forward(req,
							resp);
				} else {
					resp.getWriter().println(
							"Video is not exits or has been deleted.");
				}
			} else {
				resp.getWriter().println("Invalid video id. Try again later.");
			}
		}
	}
}
